package uk.ac.glasgow.demosocs.tests;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.VotingSystemStub;

public class EditElection {

	private VotingSystem testVS;
	
	@Before
	public void setUp() throws Exception { 
		testVS = new VotingSystemStub();
		
		testVS.setCurrentUser("admin", "p");
		testVS.createElection("EditTest", "Simple_Plurality");
		testVS.editElection(new Date(), new Date(), new Date(), "Level_3");
	}

	@After
	public void tearDown() throws Exception {
		testVS = null;
	}
	@Test
	/**test of edit election 
	  * if edit election works then the open nomination date should not be null
	  * and equal the set date
	  */
	public void testOpenNom() {		

		Date current = new Date();
		boolean result = current.toString().equals(testVS.getCurrentElection().getOpenNominations().toString());
			
		assertTrue(result);
	}

	@Test
	/**test of edit election 
	  * if edit election works then the open voting date should not be null
	  * and equal the set date
	  */
	public void testOpenVote() {		

		Date current = new Date();
		boolean result = current.toString().equals(testVS.getCurrentElection().getStart().toString());
			
		assertTrue(result);
	}
	
	@Test
	/**test of edit election 
	  * if edit election works then the end voting date should not be null
	  * and equal the set date
	  */
	public void testEndVote() {		

		Date current = new Date();
		boolean result = current.toString().equals(testVS.getCurrentElection().getEnd().toString());
			
		assertTrue(result);
	}
	

	@Test
	/**test of edit election 
	  * edit of election dates to the end before the start of 
	  * nomination should not be allowed, and should not be set
	  */
	public void testInvalidEndBeforeStart() {		

		String ID = testVS.getCurrentElection().getEID();
		
		Date current = new Date();
		Date earlier = new Date();
		earlier.setTime(earlier.getTime() - 1000000000000000000L);
		
		testVS.editElection(current, current, earlier, "Level_3");
		testVS.setCurrentElection(ID);
	
		assertFalse(earlier.equals(testVS.getCurrentElection().getEnd()));
	}
	
	@Test
	/**test of edit election 
	  * edit of election dates to the start of nomination after the end 
	  * of election should not be allowed, and should not be set
	  */
	public void testInvalidNomAfterEnd() {		

		String ID = testVS.getCurrentElection().getEID();
		
		Date current = new Date();
		Date later = new Date();
		later.setTime(later.getTime() + 1000000000000000000L);
		
		testVS.editElection(later, current, current, "Level_3");
		testVS.setCurrentElection(ID);
	
		assertFalse(later.equals(testVS.getCurrentElection().getOpenNominations()));
	}
	
	@Test
	/**test of edit election 
	  * edit of election dates to the start of nomination after the end 
	  * of election should not be allowed, and should not be set
	  */
	public void testInvalidStartBeforeNom() {		

		String ID = testVS.getCurrentElection().getEID();
		
		Date current = new Date();
		Date earlier = new Date();
		earlier.setTime(earlier.getTime() - 1000000000000000000L);
		
		testVS.editElection(current, earlier, current, "Level_3");
		testVS.setCurrentElection(ID);
	
		assertFalse(earlier.equals(testVS.getCurrentElection().getStart()));
	}
}
